package com.caigou.service.impl;

import com.caigou.bean.cms.entity.ArticleProduct;
import com.caigou.mapper.cms.ArticleProductMapper;
import com.caigou.pojo.ArticleProductData;
import com.caigou.service.ArticleProductService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ArticleProductServiceImpl implements ArticleProductService {

    @Resource
    private ArticleProductMapper articleProductMapper;

    @Override
    public ArticleProduct saveArticleProductData(String articleId, Integer channelId, String productName, String productPrice, String productModel, String productPurpose, Integer productImageId, String productImage, String productContent) {
        ArticleProduct articleProduct = new ArticleProduct();
        articleProduct.setArticleId(articleId);
        articleProduct.setChannelId(channelId);
        articleProduct.setProductName(productName);
        articleProduct.setProductPrice(productPrice);
        articleProduct.setProductModel(productModel);
        articleProduct.setProductPurpose(productPurpose);
        articleProduct.setProductImageId(productImageId);
        articleProduct.setProductImage(productImage);
        articleProduct.setProductContent(productContent);
        articleProductMapper.saveArticleProductData(articleProduct);
        return articleProduct;
    }

    @Override
    public ArticleProductData loadArticleProductData(String articleId) {
        return articleProductMapper.loadArticleProductData(articleId);
    }

    @Override
    public int updateArticleProductData(String articleId, Integer channelId, String productName, String productPrice, String productModel, String productPurpose, Integer productImageId, String productImage, String productContent) {
        ArticleProduct articleProduct = articleProductMapper.loadArticleProduct(articleId);
        boolean isCreated = false;
        if (null == articleProduct){
            isCreated = true;
            articleProduct = new ArticleProduct();
        }
        articleProduct.setArticleId(articleId);
        articleProduct.setChannelId(channelId);
        articleProduct.setProductName(productName);
        articleProduct.setProductPrice(productPrice);
        articleProduct.setProductModel(productModel);
        articleProduct.setProductPurpose(productPurpose);
        articleProduct.setProductImageId(productImageId);
        articleProduct.setProductImage(productImage);
        articleProduct.setProductContent(productContent);
        if (isCreated){
            return articleProductMapper.saveArticleProductData(articleProduct);
        } else {
            return articleProductMapper.updateArticleProductData(articleProduct);
        }
    }

    @Override
    public int deleteArticleProductByArticleId(String articleId){
        return articleProductMapper.deleteArticleProductByArticleId(articleId);
    }
}
